package com.xm.controller;

import com.xm.api.BaseResponse;
import com.xm.api.StatusCode;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import com.xm.pojo.Account;
import com.xm.service.AccountService;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.PathVariable;

@CrossOrigin
@RestController
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 明细查询 get 换成了 post ，因为前端 axios 的 get 不允许携带 @RequestBody 参数
     * @param map
     * @return
     */

    @PostMapping("/details")
    public BaseResponse details(@RequestBody Map map) {
        List<Account> accounts = accountService.selectAccountByDate(map);
        BaseResponse retMsg = new BaseResponse(StatusCode.Success);
        retMsg.setData(accounts);
        return retMsg;
    }

    /**
     * 日统计
     *
     * @return
     */
    @PostMapping("/daylist")
    public BaseResponse daylist(@RequestBody Map map) {
        List<Map> maps = accountService.selectAccountByDay(map);
        BaseResponse retMsg = new BaseResponse(StatusCode.Success);
        retMsg.setData(maps);
        return retMsg;
    }

    /**
     * 月统计
     *
     * @return
     */
    @GetMapping("/monthlist")
    public BaseResponse monthlist() {
        List<Map> maps = accountService.selectAccountByMonth();
        BaseResponse retMsg = new BaseResponse(StatusCode.Success);
        retMsg.setData(maps);
        return retMsg;
    }

    /**
     * 年统计
     *
     * @return
     */
    @GetMapping("/yearlist")
    public BaseResponse yearlist() {
        List<Map> maps = accountService.selectAccountByYear();
        BaseResponse retMsg = new BaseResponse(StatusCode.Success);
        retMsg.setData(maps);
        return retMsg;
    }

    /**
     * 插入
     *
     * @param account
     * @return
     */
    @PostMapping("/add")
    public BaseResponse add(@RequestBody Account account) {
        accountService.saveAccount(account);
        BaseResponse retMsg = new BaseResponse(StatusCode.Success);
        retMsg.setData(account.getId());
        return retMsg;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public BaseResponse delete(@PathVariable("id") Integer id) {
        accountService.removeAccount(id);
        BaseResponse retMsg = new BaseResponse(StatusCode.Success);
        return retMsg;
    }

}
